﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Thesis_v01
{
   public partial class MainApp : Form
   {
      private int childFormNumber = 0;
      private Login loginForm;
      private bool isLoggedIn;
      private int instructorID;
      private int lessonID;

      private WebBrowser webBrowser;
      private CourseList2 courseList;
      private EditLesson editLesson;
      //private AddEditQuestion addEditQuestion;
      private Size previousPanelSize;
      private Size previousContainerSize;
      //private ArrayList questions;
      private ViewResults results;
      private ViewEnquiries enquiries;

      public MainApp()
      {
         InitializeComponent();

         isLoggedIn = false;

         webBrowser = new WebBrowser();
         webBrowser.MdiParent = this;

         // hide auto-generated MDI parent template buttons
         newToolStripMenuItem.Visible = false;
         openToolStripMenuItem.Visible = false;
         toolStripSeparator3.Visible = false;
         saveToolStripMenuItem.Visible = false;
         saveAsToolStripMenuItem.Visible = false;
         toolStripSeparator4.Visible = false;
         printToolStripMenuItem.Visible = false;
         printPreviewToolStripMenuItem.Visible = false;
         printSetupToolStripMenuItem.Visible = false;
         //toolStripSeparator5.Visible = false;
         editMenu.Visible = false;
         viewMenu.Visible = false;
         toolsMenu.Visible = false;
         newWindowToolStripMenuItem.Visible = false;
         contentsToolStripMenuItem.Visible = false;
         indexToolStripMenuItem.Visible = false;
         searchToolStripMenuItem.Visible = false;
         toolStripSeparator8.Visible = false;
         newToolStripButton.Visible = false;
         openToolStripButton.Visible = false;
         saveToolStripButton.Visible = false;
         toolStripSeparator1.Visible = false;
         toolStrip1.Visible = false;
         sidebarListView.Visible = false;
         toolStripContainer1.Visible = false;
      }

      private void performLogin()
      {
         loginForm = new Login(this);
         loginForm.ShowDialog();

         if (isLoggedIn)
         {
            loginForm.Close();
         }
      }

      private void ShowNewForm(object sender, EventArgs e)
      {
         Form childForm = new Form();
         childForm.MdiParent = this;
         childForm.Text = "Window " + childFormNumber++;
         childForm.Show();
      }

      private void OpenFile(object sender, EventArgs e)
      {
         OpenFileDialog openFileDialog = new OpenFileDialog();
         openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
         openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
         if (openFileDialog.ShowDialog(this) == DialogResult.OK)
         {
            string FileName = openFileDialog.FileName;
         }
      }

      private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
      {
         SaveFileDialog saveFileDialog = new SaveFileDialog();
         saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
         saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
         if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
         {
            string FileName = saveFileDialog.FileName;
         }
      }

      private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
      {
         this.Close();
      }

      private void CutToolStripMenuItem_Click(object sender, EventArgs e)
      {
      }

      private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
      {
      }

      private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
      {
      }

      private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
      {
         toolStrip.Visible = toolBarToolStripMenuItem.Checked;
      }

      private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
      {
         statusStrip.Visible = statusBarToolStripMenuItem.Checked;
      }

      private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
      {
         LayoutMdi(MdiLayout.Cascade);
      }

      private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
      {
         LayoutMdi(MdiLayout.TileVertical);
      }

      private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
      {
         LayoutMdi(MdiLayout.TileHorizontal);
      }

      private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
      {
         LayoutMdi(MdiLayout.ArrangeIcons);
      }

      private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
      {
         foreach (Form childForm in MdiChildren)
         {
            childForm.Close();
         }
      }

      public bool IsLoggedIn
      {
         get
         {
            return isLoggedIn;
         }

         set
         {
            isLoggedIn = value;
         }
      }

      public int InstructorID
      {
         get
         {
            return instructorID;
         }

         set
         {
            instructorID = value;
         }
      }

      private void OpenNotesMenuItem_Click(object sender, EventArgs e)
      {
         OpenNotes();
      }

      private void sidebarButton_Click(object sender, EventArgs e)
      {
         ToggleSidebar();
      }

      private void ToggleSidebar()
      {
         Size currentPanelSize = toolStripContainer1.ContentPanel.Size;
         Size currentContainterSize = toolStripContainer1.Size;

         if (toolStripContainer1.ContentPanel.Visible == true)
         {
            previousPanelSize = currentPanelSize;
            previousContainerSize = currentContainterSize;
            toolStripContainer1.ContentPanel.Visible = false;
            //splitter1.Visible = false;
         }
         else
         {
            toolStripContainer1.ContentPanel.Visible = true;
            //splitter1.Visible = true;
         }

         if ((currentPanelSize.Width == 0) && (currentContainterSize.Width == 24))
         {
            toolStripContainer1.ContentPanel.Size = new System.Drawing.Size(previousPanelSize.Width, previousPanelSize.Height);
            toolStripContainer1.Size = new System.Drawing.Size(previousContainerSize.Width, previousContainerSize.Height);
         }
         else if (currentPanelSize.Width == previousPanelSize.Width)
         {
            toolStripContainer1.ContentPanel.Size = new System.Drawing.Size(0, currentPanelSize.Height);
            toolStripContainer1.Size = new System.Drawing.Size(24, currentPanelSize.Height);
         }
      }

      private void OpenNotes()
      {
         OpenFileDialog openFileDialog = new OpenFileDialog();
         openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
         openFileDialog.Filter = "Supported Files (*.pdf;*.ppt;*.pptx)|*.pdf;*.ppt;*.pptx|PDF Files (*.pdf)|*.pdf|PPT Files (*.ppt)|*.ppt|PPTX Files (*.pptx)|*.pptx";
         if (openFileDialog.ShowDialog(this) == DialogResult.OK)
         {
            string FileName = openFileDialog.FileName;

            webBrowser.Show();
            webBrowser.Navigate(FileName);
         }
      }

      private void openNotesToolStripButton_Click(object sender, EventArgs e)
      {
         OpenNotes();
      }

      private void addQuestionToolStripButton_Click(object sender, EventArgs e)
      {
         ToggleEditCourseButtonEnable();
         responsesToolStripButton.Enabled = false;
         enquiryToolStripButton.Enabled = false;
         //ToggleResponseButtonEnable();
         //ToggleEnquiryButtonEnable();
         courseList = new CourseList2();
         courseList.MdiParent = this;
         courseList.InstructorID = instructorID;
         courseList.Show();


         if (results != null)
         {
            if (results.Visible)
            {
               results.Close();
            }
         }

         if (enquiries != null)
         {
            if (enquiries.Visible)
            {
               enquiries.Close();
            }
         }
      }

      private void MainApp_Load(object sender, EventArgs e)
      {
         ToggleSidebar();

         performLogin();
      }

      public void RunLesson(int lessonID)
      {
         this.lessonID = lessonID;
         responsesToolStripButton.Enabled = true;
         enquiryToolStripButton.Enabled = true;

         ShowResponses();
         ShowEnquiries();
         //sidebarListView.Items.Clear();
         //sidebarListView.Items.Add("View Responses");
         //sidebarListView.Items.Add("Test 1");
         //sidebarListView.Items.Add("Testing 2");
         //sidebarListView.Items.Add("a");
         //sidebarListView.Items.Add("hdsfjahsdfhaskjdhfkjashfhcaskjfhakshfjk");
         //sidebarListView.Items.Add("hsdfkjahs dkjf jhasl kfjhas hfk");
         //sidebarListView.Refresh();

         //ToggleSidebar();

         // start
      }

      public void ToggleCourseListEnable()
      {
         courseList.Enabled = !courseList.Enabled;
      }

      public void ToggleEditLessonEnable()
      {
         editLesson.Enabled = !editLesson.Enabled;
      }

      public void ToggleEditCourseButtonEnable()
      {
         addQuestionToolStripButton.Enabled = !addQuestionToolStripButton.Enabled;
      }

      public void ToggleResponseButtonEnable()
      {
         responsesToolStripButton.Enabled = !responsesToolStripButton.Enabled;
      }

      public void ToggleEnquiryButtonEnable()
      {
         enquiryToolStripButton.Enabled = !enquiryToolStripButton.Enabled;
      }

      public void ToggleLogin()
      {
         if (isLoggedIn)
         {
            logoutToolStripMenuItem.Text = "Logout";
            addQuestionToolStripButton.Enabled = true;
         }
         else
         {
            logoutToolStripMenuItem.Text = "Login";
            addQuestionToolStripButton.Enabled = false;
         }
      }

      public void AddQuestion(QuestionDetail question)
      {
         editLesson.AddQuestion(question);
      }

      public void EditQuestion(QuestionDetail question)
      {
         editLesson.EditQuestion(question);
      }

      public void UpdateCourseDetail(CourseDetail newDetails)
      {
         courseList.UpdateCourseDetail(newDetails);
      }

      public EditLesson SetEditLesson
      {
         set
         {
            editLesson = value;
         }
      }

      //public ArrayList Questions
      //{
      //   set
      //   {
      //      questions = value;
      //   }
      //}

      private void sidebarListView_DoubleClick(object sender, EventArgs e)
      {
         //MessageBox.Show("blah");
         int selectedIndex = sidebarListView.SelectedIndices[0];
         string selectedItem = sidebarListView.SelectedItems[selectedIndex].Text;
         if (selectedItem == "View Responses")
         {
            results = new ViewResults(lessonID);
            results.MdiParent = this;

            results.Show();
         }
      }

      private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
      {
         performLogin();
      }

      private void responsesToolStripButton_Click(object sender, EventArgs e)
      {
         //addQuestionToolStripButton.Enabled = false;
         //responsesToolStripButton.Enabled = false;
         //results = new ViewResults(lessonID);
         //results.MdiParent = this;

         //results.Show();
         ShowResponses();
      }



      private void enquiryToolStripButton_Click(object sender, EventArgs e)
      {
         ShowEnquiries();
      }

      private void ShowResponses()
      {
         //addQuestionToolStripButton.Enabled = false;
         responsesToolStripButton.Enabled = false;
         results = new ViewResults(lessonID);
         results.MdiParent = this;

         results.Show();
      }

      private void ShowEnquiries()
      {
         //addQuestionToolStripButton.Enabled = false;
         enquiryToolStripButton.Enabled = false;
         enquiries = new ViewEnquiries(lessonID);
         enquiries.MdiParent = this;

         enquiries.Show();
      }

      //public void SetAddEditQuestion
      //{
      //   set
      //   {
      //      addEditQuestion = value;
      //   }
      //}
   }
}
